package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.cust.ContextCustDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.goods.IGoodsService;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.goods.GoodsDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalQuerySeqHelper;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.enums.RelType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.bo.UserInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <Description> 云账号分类 <br>
 *
 * @version 1.0<br>
 * @see com.iwhalecloud.bss.kite.web.service.accept.attr.handler <br>
 * @since R9.0<br>
 */
@Service
public class CloudAccountCateCommonAttrHandler extends AbstractAttrHandler {
    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOperateSceneInstService operateSceneInstService;
    @Autowired
    private OperateInstAttrService operateInstAttrService;
    @Autowired
    private IQuerySceneInstService sceneInstQueryService;
    @Autowired
    private IOperateSceneInstService operateInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {

    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isEmpty(value)) {
            operateSceneCommonService.setCommonAttrValue(sceneInstId, "business_number", "", "");
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        // 序列
        Map<String, String> inParam = new HashMap<>();
        ContextCustDTO custDTO1 = KiteWebContext.getContext().getCust();
        CuccContextCustDTO custDTO = KiteBeanUtils.transform(custDTO1, CuccContextCustDTO.class);
        if (KiteStringUtils.isEmpty(custDTO.getEparchyCode())) {
            custDTO.setEparchyCode(ContextUtil.getLoginInfo().getUserInfo().getPostLanId());
        }
        String cateId = "";
        String prodId = "";
        if (KiteStringUtils.equals("10002984", value)) {
            cateId = "2100982";
            prodId = "10002984";
        } else if (KiteStringUtils.equals("10002985", value)) {
            cateId = "2100983";
            prodId = "60000483";
        } else {
            cateId = "2000998";
            prodId = "50000003";
        }
        List<GoodsDTO> goodsList = goodsService.queryGoodsListInCategroyCloud(cateId, null);

        String offerId = "";
        String goodsName = "";
        if (KiteListUtils.isNotEmpty(goodsList)) {
            for (GoodsDTO goods : goodsList) {
                offerId = goods.getGoodsSku();
                goodsName = goods.getGoodsName();
            }
        }
        inParam.put("SEQ_TYPE", "SERIAL_NUMBER");
        inParam.put("SYS_TYPE", "1");
        inParam.put("SYS_SOURCE", "");
        inParam.put("NEW_OFFER_ID", offerId);
        inParam.put("CATE_ID", cateId);
        // 调接口生成号码
        String seq = LocalQuerySeqHelper.getSeqBySeqType(inParam, sceneInstId, custDTO);
        if (KiteStringUtils.isEmpty(seq)) {
            seq = LocalKeyConsts.EMPTY_SERIAL_NUMBER;
        }
        operateSceneCommonService.setCommonAttrValue(sceneInstId, "business_number", seq, seq);
        // 实例处理
        String rootInstId = this.querySceneInstService.getSceneInst(sceneInstId).getRootInstId();
        InstType instType = this.querySceneInstService.getSceneInst(sceneInstId).getRootInstType();
        KiteInst kiteInst = this.querySceneInstService.getKiteInst(custDTO.getCustId(), instType, rootInstId);

        String subGoodsSku="";
        String subGoodsName="";
        Map<String, KiteAttr> attrMap = kiteInst.getAttrMap();
        Map<String, Object> goodsByOfferId = goodsService.queryGoodsByOfferId(offerId);
        String cateName = KiteMapUtils.getString(goodsByOfferId, "cateName");
        List<Map<String, Object>> saleGoods = KiteMapUtils.getValue(goodsByOfferId, "saleGoods");
        if (KiteListUtils.isNotEmpty(saleGoods)) {
            for (Map<String, Object> map : saleGoods) {
                subGoodsSku = KiteMapUtils.getString(map, "goodsSku");
                subGoodsName = KiteMapUtils.getString(map, "goodsName");
            }
        }
        kiteInst.setSpecId(subGoodsSku);
        kiteInst.setSpecName(subGoodsName);

        if (KiteMapUtils.isNotEmpty(attrMap)) {
            KiteAttr saleGoodsNameAttr = attrMap.get("saleGoodsName");
            saleGoodsNameAttr.setValue(goodsName);
            saleGoodsNameAttr.setValueDesc(goodsName);
            saleGoodsNameAttr.setDefaultValue(goodsName);

            KiteAttr cateIdAttr = attrMap.get("cateId");
            cateIdAttr.setValue(cateId);
            cateIdAttr.setValueDesc(cateId);
            cateIdAttr.setDefaultValue(cateId);

            KiteAttr saleGoodsCodeAttr = attrMap.get("saleGoodsCode");
            saleGoodsCodeAttr.setValue(offerId);
            saleGoodsCodeAttr.setValueDesc(offerId);
            saleGoodsCodeAttr.setDefaultValue(offerId);

            KiteAttr cateNameAttr = attrMap.get("cateName");
            cateNameAttr.setValue(cateName);
            cateNameAttr.setValueDesc(cateName);
            cateNameAttr.setDefaultValue(cateName);
        }

        List<KiteProdInst> prodInsts = sceneInstQueryService.listProdInstsFromOfferInst(custDTO.getCustId(), kiteInst.getInstId());
        List<OfferDetail> details = OfferCache.get(offerId).listAccessProdDetail();
        if (KiteListUtils.isNotEmpty(details)) {
            for (OfferDetail detail : details) {
                if (KiteListUtils.isNotEmpty(prodInsts)) {
                    for (KiteProdInst kiteProdInst : prodInsts) {
                        kiteProdInst.setRoleId(detail.getRoleId());
                        kiteProdInst.setOfferId(offerId);
                        kiteProdInst.setSpecId(prodId);
                        Map<String, KiteAttr> prodAttrMap = kiteProdInst.getAttrMap();
                        if (KiteMapUtils.isNotEmpty(prodAttrMap)) {
                            KiteAttr prodCodeAttr = prodAttrMap.get("200000106");
                            prodCodeAttr.setValue(value);
                            prodCodeAttr.setValueDesc(value);
                            prodCodeAttr.setDefaultValue(value);
                        }
                        operateSceneInstService.addProdInst(sceneInstId, kiteProdInst);
                        operateInstService.addOfferObjInstRel(sceneInstId, kiteProdInst.getInstType(), kiteProdInst.getInstId(), kiteProdInst.getInstType(),
                            kiteProdInst.getProdInstId(), RelType.OFFER_OBJ_REL_TYPE_1000, ActionType.A, detail.getRoleId(), detail.getDetailId(),
                            kiteProdInst.getOwnerCustId());
                    }
                }
            }
        }

        UserInfo userInfo = ContextUtil.getLoginInfo().getUserInfo();

        //是否总部收费
        KiteAttr attr1 = querySceneInstService.getCommonAttr(sceneInstId, "ORD10125");
        //是否总部直签
        KiteAttr attr2 = querySceneInstService.getCommonAttr(sceneInstId, "ORD10129");
        List<KiteAttr> kiteAttrList=new ArrayList<>();
        kiteAttrList.add(attr1);
        kiteAttrList.add(attr2);

        for(KiteAttr kiteAttr:kiteAttrList){
            List<KiteAttrValue> attrDtos = kiteAttr.getAttrValues();
// 国际业务48只能选否，49只能选是
            if(KiteStringUtils.isNotEmpty(userInfo.getPostProvinceNbr())){
                if(userInfo.getPostProvinceNbr().startsWith("48")){
                    attrDtos = attrDtos.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(),"1_0000107","0")).collect(Collectors.toList());
                    if(KiteListUtils.isNotEmpty(attrDtos)){
                        KiteAttrValue attrDO = attrDtos.get(0);
                        kiteAttr.setValue(attrDO.getAttrValue());
                        kiteAttr.setValueDesc(attrDO.getAttrValueDesc());
                        kiteAttr.setAttrValues(attrDtos);
                    }

                }else  if(userInfo.getPostProvinceNbr().startsWith("49")){
                    attrDtos = attrDtos.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(),"1_0000106","1")).collect(Collectors.toList());
                    if(KiteListUtils.isNotEmpty(attrDtos)){
                        KiteAttrValue attrDO = attrDtos.get(0);
                        kiteAttr.setValue(attrDO.getAttrValue());
                        kiteAttr.setValueDesc(attrDO.getAttrValueDesc());
                        kiteAttr.setAttrValues(attrDtos);
                    }
                }
            }
            operateSceneInstService.setCommonAttr(sceneInstId, kiteAttr);
        }




        operateInstAttrService.setAttrValue(kiteInst, "200000106", prodId, sceneInstId, null);
        operateInstAttrService.setAttrValue(kiteInst, "saleGoodsName", goodsName, sceneInstId, null);
        operateInstAttrService.setAttrValue(kiteInst, "cateId", cateId, sceneInstId, null);
        operateInstAttrService.setAttrValue(kiteInst, "saleGoodsCode", offerId, sceneInstId, null);
        operateInstAttrService.setAttrValue(kiteInst, "cateName", cateName, sceneInstId, null);
        operateSceneInstService.addInst(sceneInstId, kiteInst);

        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
